from app import app,request,basedir,json,url_for,session,g
import os,sys,random,string
from werkzeug import secure_filename
from app.model import db,Index_table,Brands_table,Shop_information,User_table,Index_video,Admin_media,Contact_us,Vip_center,Brands_img,Admin_lookbook,Contact_way
from app.controller.tools import usual_save_img,res_json,baseUrl



@app.after_request
def add_header(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response

@app.route('/',methods=['GET'])
def hello():
    return 'hello'

#登录
@app.route('/api/v1/admin_login',methods=['POST'])
def admin_login():
    username = request.form.get('username')
    password = request.form.get('password')
    resData = {}
    result = User_table.query.filter_by(username = username).first()
    if result:
        if User_table.query.filter_by(password = password).first():
            token = ''.join(random.sample(string.ascii_letters + string.digits, 16))
            result.token = token
            db.session.commit()
            resData = {
                'code':200,
                'data':{
                    'username':username,
                    'token':token
                }
            }
        else:
            resData = {
                'code':201,
                'msg':'密码错误'
            }
    else:
        resData = {
            'code':201,
            'msg':'账号错误'
        }
    return json.dumps(resData,ensure_ascii = False)

#修改密码
@app.route('/api/v1/change_pw',methods=['POST'])
def change_pw():
    y_pw = request.form.get('y_pw')
    n_pw = request.form.get('n_pw')
    result = User_table.query.filter_by(username='admin').first()
    if(result.password == y_pw):
        result.password = n_pw
        db.session.commit()
        return res_json(200,'操作成功','')
    else:
        return res_json(201,'原密码输入错误','')


#首页 大图 接收数据
@app.route('/api/v1/upload',methods=['GET','POST'])
def up_photo():
    img = request.files.get('add_pic')
    pic_url = request.form.get('pic_url')
    url = usual_save_img(img)
    #存入数据库
    db_save = Index_table(img_file=url,img_url=pic_url)
    db.session.add(db_save)
    db.session.commit()
    return url,200

#首页 返回数据
@app.route('/api/v1/getIndex',methods=['GET','POST'])
def getIndexData():
    results = Index_table.query.all()
    resData = []
    for x in results:
        resData.append({
            'id':x.id,
            'img_file':baseUrl+x.img_file,
            'img_url':x.img_url
        })
    return json.dumps(resData,ensure_ascii=False)

#首页 修改数据
@app.route('/api/v1/changeData',methods=['GET','POST'])
def changeData():
    ID = request.form.get('res_id')
    img_url = request.form.get('pic_url')
    img = request.files.get('add_pic')
    results = Index_table.query.filter_by(id=ID).first()
    if img == None:
        print('为空')
    else:
        url = usual_save_img(img)
        #数据库操作 修改
        results.img_file = url
    results.img_url = img_url        
    db.session.commit()
    return 'success',200

#首页 删除数据
@app.route('/api/v1/index_del',methods=['GET','POST'])
def index_del():
    del_id = request.form.get('del_id')
    results = Index_table.query.filter_by(id=del_id).first()
    db.session.delete(results)
    db.session.commit()
    return 'success',200


#首页 接收视频数据
@app.route('/api/v1/sendData',methods = ['POST'])
def sendData():
    video_id = request.form.get('video_id')
    video_tle = request.form.get('video_tle')
    video_tleen = request.form.get('video_tleen')
    video_img = request.files.get('video_img')
    video_file = request.files.get('video_file')

    if video_id == None:
        print('新增new')
        #新增
        img_url = usual_save_img(video_img)
        video_url = usual_save_img(video_file)
        #存入数据库
        db_data = Index_video(video_tle=video_tle,video_tleen=video_tleen,video_img=img_url,video_file=video_url)
        db.session.add(db_data)
        db.session.commit()
        return 'success',200  

    else:
        db_resuklt = Index_video.query.filter_by(id=video_id).first()
        if not video_img == None:
            img_url_s = usual_save_img(video_img)
            db_resuklt.video_img = img_url_s
        if not video_file == None:
            img_files_s = usual_save_img(video_file)
            db_resuklt.video_file = img_files_s

        db_resuklt.video_tle = video_tle
        db_resuklt.video_tleen = video_tleen
        db.session.commit()
        return 'success',200
        



#首页 视频 返回数据
@app.route('/api/v1/getvideo',methods=['POST'])
def getvideo():
    results = Index_video.query.all()
    resData = []
    for x in results:
        resData.append({
            'id':x.id,
            'video_tle':x.video_tle,
            'video_tleen':x.video_tleen,
            'video_img':baseUrl+x.video_img,
            'video_file':baseUrl+x.video_file
        })
    return json.dumps(resData,ensure_ascii=False)

#首页 视频 删除数据
@app.route('/api/v1/delete_video',methods=['POST'])
def delete_video():
    del_id = request.form.get('del_id')
    result = Index_video.query.filter_by(id=del_id).first()
    db.session.delete(result)
    db.session.commit()
    return 'success',200


#品牌介绍 店铺信息 （ckeditor编辑器）接受图片
@app.route('/api/imgupload/ckeditorUpload',methods=['GET','POST'])
def editorData():
    img = request.files.get('upload')
    url = usual_save_img(img)
    resData = {
        "uploaded": 1,
        "fileName": img.filename,
        "url": baseUrl+url
    }
    return json.dumps(resData,ensure_ascii=False)

#品牌介绍 保存数据
@app.route('/api/v1/brand_save',methods=['POST'])
def brand_save():
    head_tle = request.form.get('head_tle')
    img_txt = request.form.get('img_txt')
    big_img = request.files.get('big_img')
    results = Brands_table.query.filter_by(id=1).first()
    if not big_img == None:
        img_url = usual_save_img(big_img)
        results.big_img = img_url
   
    results.head_tle = head_tle
    results.img_txt = img_txt
    db.session.commit()
    return 'success',200

#品牌介绍 错位图文字保存
@app.route('/api/v1/brand_save_txt',methods=['POST'])
def brand_save_txt():
    md_tle = request.form.get('md_tle')
    bot_tle = request.form.get('bot_tle')

    results = Brands_table.query.filter_by(id=1).first()
    results.md_tle = md_tle
    results.bot_tle = bot_tle
    db.session.commit()
    return 'success',200


#品牌介绍 返回数据
@app.route('/api/v1/brand_getData',methods=['POST'])
def brand_getData():
    results = Brands_table.query.filter_by(id=1).first()
    resData = []
    resData.append({
        'head_tle':results.head_tle,
        'img_txt':results.img_txt,
        'big_img':baseUrl+ results.big_img,
        'md_tle':results.md_tle,
        'bot_tle':results.bot_tle
        })
    return res_json(200,'success',resData)

#品牌介绍 接收错位图
@app.route('/api/v1/brand_img_cw',methods=['POST'])
def brand_img_cw():
    img = request.files.get('add_pic')
    img_index = request.form.get('pic_index')
    img_url = usual_save_img(img)
    img_save = Brands_img(urls = img_url,index = img_index)
    db.session.add(img_save)
    db.session.commit()
    return 'success'

#品牌介绍 返回错位图
@app.route('/api/v1/get_img_cw',methods=['POST'])
def get_img_cw():
    
    results = Brands_img.query.order_by(Brands_img.index).all()
    resData = []
    for item in results:
        resData.append({
            'img_url':baseUrl + item.urls,
            'id':item.id,
            'index':item.index
        })
    return res_json(200,'成功',resData)

#品牌介绍 删除错位图
@app.route('/api/v1/brand_del',methods=['POST'])
def brand_del():
    img_id = request.form.get('img_id')
    result = Brands_img.query.filter_by(id = img_id).first()
    db.session.delete(result)
    db.session.commit()
    return 'success'

# #在线商城 返回数据
# @app.route('/api/v1/shop_online_getdata',methods=['POST'])
# def shop_online_getdata():
#     results = Shop_information.query.filter_by(id=2).first()
#     return results.content,200

#社交媒体 保存数据
@app.route('/api/v1/admin_media',methods=['POST'])
def admin_media_func():
    media_img = request.files.get('media_img')
    img_url = usual_save_img(media_img)
    results = Admin_media.query.filter_by(id=1).first()
    results.content = img_url
    db.session.commit()
    return 'success',200

#社交媒体 返回数据
@app.route('/api/v1/get_media',methods=['POST'])
def get_media():
    results = Admin_media.query.filter_by(id=1).first()
    img_url = baseUrl + results.content
    return img_url,200

#会员中心 保存数据
@app.route('/api/v1/admin_vip',methods=['POST'])
def admin_vip():
    brands_data = request.form.get('content')
    results = Vip_center.query.filter_by(id=1).first()
    results.content = brands_data
    db.session.commit()
    return 'success',200

#会员中心 返回数据
@app.route('/api/v1/get_vip',methods=['POST'])
def get_vip():
    results = Vip_center.query.filter_by(id=1).first()
    return results.content,200

#联系我们 保存数据
@app.route('/api/v1/contact_us',methods=['POST'])
def contact_us():
    contact_icon = request.files.get('contact_icon')
    img_url = usual_save_img(contact_icon)
    contact_name = request.form.get('contact_name')
    contact_way = request.form.get('contact_way')
    result = Contact_way(icon=img_url,tle=contact_name,cont=contact_way)
    db.session.add(result)
    db.session.commit()
    return 'success',200

#联系我们 返回数据
@app.route('/api/v1/get_contact',methods=['POST'])
def get_contact():
    result = Contact_way.query.all()
    resData = []
    for item in result:
        resData.append({
            'id':item.id,
            'tle':item.tle,
            'icon':baseUrl+item.icon,
            'cont':item.cont
        })
    return res_json(200,'success',resData)

#联系我们 删除数据
@app.route('/api/v1/contact_del',methods=['POST'])
def contact_del():
    res_id = request.form.get('res_id')
    reult = Contact_way.query.filter_by(id = res_id).first()
    db.session.delete(reult)
    db.session.commit()
    return 'success'

#lookbook 保存数据
@app.route('/api/v1/Admin_lookbook',methods=['POST'])
def Admin_lookbooks():
    brands_data = request.form.get('content')
    results = Admin_lookbook.query.filter_by(id=1).first()
    results.content = brands_data
    db.session.commit()
    return 'success',200


#lookbook 返回数据
@app.route('/api/v1/lookbook_getdata',methods=['POST'])
def lookbook_getdata():
    results = Admin_lookbook.query.filter_by(id=1).first()
    return results.content,200

#联系我们 背景图保存数据
@app.route('/api/v1/contact_bg',methods=['POST'])
def contact_bg():
    media_img = request.files.get('media_img')
    img_url = usual_save_img(media_img)
    results = Contact_us.query.filter_by(id=1).first()
    results.content = img_url
    db.session.commit()
    return 'success',200

#联系我们 背景图返回数据
@app.route('/api/v1/contact_bg_getData',methods=['POST'])
def contact_bg_getData():
    results = Contact_us.query.filter_by(id=1).first()
    img_url = baseUrl + results.content
    return img_url,200